﻿# Culture on actions.

# Happens once a year for the culture. Arbitrary day, but the same day every year for that culture
# Root is the culture
yearly_culture_pulse = {
	random_events = {
		chance_to_happen = 5
		100 = culture_tradition_events.0099 # Head of Faith asks you to remove a tradition you shouldn't have
	}

}

# Happens once every three years for the culture. Arbitrary day, but the same day every year for that culture. Same day as yearly_culture_pulse
# Root is the culture
three_yearly_culture_pulse = {
	random_events = {
		chance_to_happen = 10
		100 = culture_emergence_events.0003
	}
}

# Happens when the culture reaches a new era
# Root is the culture
on_culture_era_changed = {
	
}

# Happens when a character changes culture
# Root = character
on_character_culture_change = {
	effect = {
		every_held_title = {
#			update_dynamic_coa = yes
		}
		if = {
			limit = {
				is_ruler = yes
				should_have_same_faith_succession_trigger = yes
				NOT = {
					has_realm_law = same_faith_only_law
				}
			}
			add_realm_law_skip_effects = same_faith_only_law
		}

		# Struggle Catalyst: heir changes culture while under age
		if = {
			limit = {
				catalyst_very_important_child_change_culture_or_faith_preliminary_trigger = yes
			}
			random_character_struggle = { 
				involvement = involved
				limit = {
				phase_has_catalyst = catalyst_very_important_child_change_culture_or_faith
				}
				activate_struggle_catalyst = {
					catalyst = catalyst_very_important_child_change_culture_or_faith
					character = root
				}
			}
		}

		# Hostage story cycle
		if = {
			limit = {
				is_hostage = yes
				is_adult = no
				# Did they just change to their captor's culture
				culture = warden.culture
			}
			save_scope_as = hostage
			warden = {
				trigger_event = bp2_yearly.8050
			}
		}
	}
}

# Happens when a culture loses a tradition
# Root = culture
# scope:tradition = flag for the tradition removed
on_tradition_removed = {
	effect = {
		every_player = {
			limit = {
				culture = root
			}
			trigger_event = {
				id = culture_notification.0010
				days = 0
			}
		}
	}	
}

# Happens when a culture gains a tradition
# Root = culture
# scope:tradition = flag for the tradition added
on_tradition_added = {
	effect = {
		every_player = {
			limit = {
				culture = root
			}
			trigger_event = {
				id = culture_notification.0011
				days = 0
			}
		}
		
		every_ruler = {
			limit = {
				is_ruler = yes
				should_have_same_faith_succession_trigger = yes
				NOT = {
					has_realm_law = same_faith_only_law
				}
			}
			add_realm_law_skip_effects = same_faith_only_law
		}

		set_variable = { name = tradition_cooldown years = add_tradition_cooldown }
	}
}

# Happens when a culture is created through hybridization or divergence
# Root = The new culture
# scope:founder = The founder of the new culture
on_culture_created = {
	effect = {
		# Set up the parent culture(s) as scope(s).
		random_parent_culture = { save_scope_as = parent_culture_1 }
		if = {
			limit = {
				any_parent_culture = { count >= 2 }
			}
			random_parent_culture = {
				limit = {
					NOT = { this = scope:parent_culture_1 }
				}
				save_scope_as = parent_culture_2
			}
		}

		# General effects
		new_culture_created_conversion_effect = {
			CONVERTEE = scope:founder
			CONVERTER = scope:founder
			CULTURE = root
		}
		if = {
			limit = {
				scope:founder = {
					NOT = { has_character_flag = suppress_culture_notifications_and_achievements }
				}
			}
			if = {
				limit = {
					is_hybrid_culture = yes
				}
				set_variable = {
					name = ep1_02_achievement_unlocked
					value = 1
				}
				# Save scopes for toast localization
				save_scope_as = new_culture
				random_parent_culture = {
					save_scope_as = parent_1
				}
				random_parent_culture = {
					limit = { NOT = { this = scope:parent_1 } }
					save_scope_as = parent_2
				}

				# Send toast event to all relevant players
				every_player = {
					if = {				
						limit = {
							culture = {	save_temporary_scope_as = player_culture }
							root = {
								# Player culture matches new culture or the parent
								OR = {
									this = scope:player_culture
									any_parent_culture = {
										this = scope:player_culture
									}
								}
							}
						}
						trigger_event = {
							id = culture_notification.1011
							days = 0
						}
					}
					else_if = {
						limit = {
							in_diplomatic_range = scope:founder
						}
						trigger_event = {
							id = culture_notification.1001
							days = 0
						}
					}
				}
				if = {
					limit = {
						scope:founder = {
							has_title = title:k_jerusalem
							religion = religion:christianity_religion
							religion = { exists = var:variable_ghw_unlocked }
						}
						is_hybrid_culture = yes
						NOT = {
							has_innovation = innovation_desert_tactics
						}
						any_parent_culture = {
							has_cultural_pillar = heritage_arabic
						}
					}
					add_innovation = innovation_desert_tactics
				}
			}
			else_if = {
				limit = {
					is_divergent_culture = yes
				}
				set_variable = {
					name = ep1_03_achievement_unlocked
					value = 1
				}
				# Save scopes for toast localization
				save_scope_as = new_culture
				random_parent_culture = {
					save_scope_as = parent_1
				}

				# Send toast event to all relevant players
				every_player = {
					if = {				
						limit = {
							culture = {	save_temporary_scope_as = player_culture }
							root = {
								# Player culture matches new culture or the parent
								OR = {
									this = scope:player_culture
									any_parent_culture = {
										this = scope:player_culture
									}
								}
							}
						}
						trigger_event = {
							id = culture_notification.1111
							days = 0
						}
					}
					else_if = {
						limit = {
							culture = { save_temporary_scope_as = player_culture }
							root = {
								OR = {
									has_same_culture_heritage = scope:player_culture
									any_parent_culture = {
										has_same_culture_heritage = scope:player_culture
									}
								}
							}
						}
						trigger_event = {
							id = culture_notification.1101
							days = 0
						}
					}
				}
			}
			if = {
				limit = {
					is_divergent_culture = yes
					scope:founder = {
						is_ai = no
						OR = {
							primary_title = title:c_fika
							AND = {
								has_title = title:c_fika
								capital_county ?= title:c_fika
							}
						}
					}
				}
				scope:founder = {
					add_achievement_global_variable_effect = {
						VARIABLE = diverged_culture_nobody_comes_to_fika_achievement
						VALUE = yes
					}
				}
			}
			if = {
				limit = {
					is_divergent_culture = yes
					scope:founder = {
						is_ai = no
						faith.religion = religion:judaism_religion
						capital_province = { geographical_region = world_africa_east }
						primary_title.tier >= tier_kingdom
					}
				}
				scope:founder = {
					add_achievement_global_variable_effect = {
						VARIABLE = diverged_culture_beta_israel_achievement
						VALUE = yes
					}
				}
			}
			# Struggle Catalysts
			scope:founder = {
				if = {
					limit = {
						any_character_struggle = {
							involvement = involved
							catalyst_hybridise_or_diverge_regional_cultures_preliminary_trigger = yes
						}
					}
					every_character_struggle = {
						involvement = involved
						limit = {
							catalyst_hybridise_or_diverge_regional_cultures_preliminary_trigger = yes
						}
						activate_struggle_catalyst = {
							catalyst = catalyst_hybridise_or_diverge_regional_cultures
							character = scope:founder
						}
					}
				}
			}
			# If you have a legend maybe we change it?
			if = {
				limit = {
					scope:founder = {
						has_dlc_feature = legends
						exists = promoted_legend
						promoted_legend.legend_owner = scope:founder
						promoted_legend = {
							has_legend_chapter = opening
						}
						NOT = {
							promoted_legend ?= {
								has_legend_chapter = {
									name = opening
									localization_key = legend_chapter_opening_new_culture
								}
							}
						}
					}
				}
				scope:founder = {
					random = {
						chance = 75
						send_interface_toast = {
							title = legend_chapter_change_culture_toast
							left_icon = scope:founder
							promoted_legend = {
								set_legend_chapter = {
									name = opening
									localization_key = legend_chapter_opening_new_culture
								}
							}
						}
					}
				}
			}
		}
	}
}

# Happens when a county is granted to an auto-generated noble of the same culture as the ruler
# Root = The culture
# scope:actor = The granter
# scope:landed_title = The landed title
on_county_auto_granted_to_liege_culture = {
	effect = {
		hidden_effect = {
			scope:landed_title.holder = {
				save_scope_as = recipient
			}
			scope:landed_title = {
				save_scope_as = loc_title
			}
			if = {
				limit = {
					scope:landed_title.tier = tier_barony
				}
				scope:landed_title.holder = {
					add_opinion = {
						target = scope:actor
						modifier = received_title_barony
					}
				}
			}
			else_if = {
				limit = {
					scope:landed_title.tier = tier_county
				}
				scope:landed_title.holder = {
					add_opinion = {
						target = scope:actor
						modifier = received_title_county
					}
				}
			}
			scope:actor = {
				if = {
					limit = {
						government_has_flag = government_is_nomadic
						vassal_limit_available <= 0
						is_independent_ruler = yes
					}
					send_interface_toast = {
						type = event_toast_effect_neutral
						title = grant_titles_interaction_notification
						
						left_icon = scope:actor
						right_icon = scope:recipient
						
						custom_tooltip = grant_titles_interaction_notification_effect_7
					}
				}
				else = {
					send_interface_toast = {
						type = event_toast_effect_neutral
						title = grant_titles_interaction_notification
					
						left_icon = scope:actor					
						right_icon = scope:recipient
					
						custom_tooltip = grant_titles_interaction_notification_effect_2
					}
				}
			}

			# Struggle parameter
		 	 scope:actor = {
		 	 	if = {
					limit = {
						any_character_struggle = {
							involvement = involved
							has_struggle_phase_parameter = granting_title_to_local_noble_gives_prestige
						}
					}
					send_interface_toast = {
						type = event_toast_effect_good
						title = prestige_from_granting_title.tt
	 					left_icon = scope:actor
						add_prestige = medium_prestige_gain
					}
				}
		  	}

			# Struggle Catalyst
			if = {
				limit = {
					scope:landed_title = {
						any_county_struggle = {
							any_involved_ruler = {
								this = scope:actor
							}
						}
					}
					scope:actor = {
						any_character_struggle = {
							involvement = involved
							phase_has_catalyst = catalyst_grant_land_local_noble
							is_culture_involved_in_struggle = scope:recipient.culture
						}
					}
				}
				scope:actor = {
					every_character_struggle = {
						involvement = involved
						activate_struggle_catalyst = {
							catalyst = catalyst_grant_land_local_noble
							character = scope:actor
						}
					}
				}
			}
		}
	}
}

# Happens when a county is granted to an auto-generated local culture noble
# Root = The culture
# scope:actor = The granter
# scope:landed_title = The landed title
on_county_auto_granted_to_local_culture = {
	effect = {
		hidden_effect = {
			scope:landed_title.holder = {
				save_scope_as = recipient
			}
			scope:landed_title = {
				save_scope_as = loc_title
			}
			scope:actor = {
				send_interface_toast = {
					type = event_toast_effect_neutral
					title = grant_titles_interaction_notification
				
					left_icon = scope:actor					
					right_icon = scope:recipient
				
					custom_tooltip = grant_titles_interaction_notification_effect_2
				}
			}
			if = {
				limit = {
					scope:landed_title.tier = tier_barony
				}
				scope:landed_title.holder = {
					add_opinion = {
						target = scope:actor
						modifier = received_title_barony
					}
				}
			}
			else_if = {
				limit = {
					scope:landed_title.tier = tier_county
				}
				scope:landed_title.holder = {
					add_opinion = {
						target = scope:actor
						modifier = received_title_county
					}
				}
			}
			if = {
				limit = {
					exists = scope:actor.dynasty
					scope:actor.dynasty = {
						has_dynasty_perk = ep1_culture_legacy_3
					}
				}
				scope:actor = {
					if = {
						limit = {
							can_add_hook = {
								target = scope:recipient
								type = favor_hook
							}
						}
						add_hook = {
							type = favor_hook
							target = scope:recipient
						}
					}
				}
			}

			# Struggle parameter
		 	 scope:actor = {
		 	 	if = {
					limit = {
						any_character_struggle = {
							involvement = involved
							has_struggle_phase_parameter = granting_title_to_local_noble_gives_prestige
						}
					}
					send_interface_toast = {
						type = event_toast_effect_good
						title = prestige_from_granting_title.tt
	 					left_icon = scope:actor
						add_prestige = medium_prestige_gain
					}
				}
		  	}

			# Struggle Catalyst
			if = {
				limit = {
					scope:landed_title = {
						any_county_struggle = {
							any_involved_ruler = {
								this = scope:actor
							}
						}
					}
					scope:actor = {
						any_character_struggle = {
							involvement = involved
							phase_has_catalyst = catalyst_grant_land_local_noble
							is_culture_involved_in_struggle = scope:recipient.culture
						}
					}
				}
				scope:actor = {
					every_character_struggle = {
						involvement = involved
						activate_struggle_catalyst = {
							catalyst = catalyst_grant_land_local_noble
							character = scope:actor
						}
					}
				}
			}
		}
		change_cultural_acceptance = {
			target = scope:actor.culture
			value =	auto_grant_county_cultural_acceptance_impact
			desc = cultural_acceptance_gain_auto_granted_county
		}
	}
}

# Happens when a county is granted to an auto-generated herder
# root = The landed title
# scope:actor = The nomad
# scope:recipient = the herder
on_county_auto_granted_to_herder = {

}
